package com.navi.alarm.controller;

import com.alibaba.fastjson.JSON;
import com.navi.alarm.service.SendMailService;
import com.navi.alarm.tx.amssender.AmssenderI;
import com.navi.alarm.tx.amssender.AmssenderO;
import com.navi.alarm.tx.amstx.AmsTxI;
import jdk.nashorn.internal.parser.JSONParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Controller
//@RequestMapping("/api")
@Slf4j
public class SendMailController {

    @Value("spring.mail.username")
    private String addressFrom;

    @Value("alarm_system_from_name")
    private String mailFromName;


    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private SendMailService sendMailService;


    @Autowired
    TemplateEngine templateEngine;

    @RequestMapping(value = "/goto", method = RequestMethod.GET)
    public String goToPageFunc(String pageNo) {
        log.info("goto page : {}", pageNo);
        return pageNo;
    }

    @RequestMapping(value = "/main", method = RequestMethod.GET)
    public String pageFunc() {
        return "main";
    }

    @RequestMapping(value = "/testXml", method = RequestMethod.GET, produces = MediaType.APPLICATION_ATOM_XML_VALUE)
    @ResponseBody
    public AmssenderO testXMl(AmssenderI inTrx) {
        System.out.println(inTrx.getTrx_id());
        System.out.println("123456");
        AmssenderO outTrx = new AmssenderO();
        outTrx.setRtn_code("000000");
        outTrx.setRtn_mesg("aaibbsuccess1");
        return outTrx;
    }

    @RequestMapping(value = "/sendMail",  produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String sendSimpleMessage(String inObj) {
        System.out.println(inObj);
        log.info("inObj : {}", inObj);

        AmsTxI inTrx = JSON.parseObject(inObj, AmsTxI.class);
        sendMailService.subMainProc(inTrx);

        return "Mail Sent";

    }

    @RequestMapping(value = "/sendMimeMsg", method = RequestMethod.GET)
    @ResponseBody
    public String sendAttachementsMail() throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("372086794@qq.com");
        helper.setTo("372086794@qq.com");
        helper.setSubject("主题：有附件");
        helper.setText("有附件的邮件");

        FileSystemResource file = new FileSystemResource(new File("E:/test.jpg"));
        helper.addAttachment("附件-1.jpg", file);
        helper.addAttachment("附件-2.jpg", file);

        mailSender.send(mimeMessage);

        return "MimeMail send";
    }

    @RequestMapping(value = "/sendInlineMail", method = RequestMethod.GET)
    @ResponseBody
    public String sendInlineMail() throws Exception {

        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("372086794@qq.com");
        helper.setTo("372086794@qq.com");
        helper.setSubject("主题：嵌入静态资源");
        helper.setText("<html><body><img src=\"cid:xinba\" ></body></html>", true);

        FileSystemResource file = new FileSystemResource(new File("E:/xinba.jpg"));
        helper.addInline("xinba", file);

        mailSender.send(mimeMessage);

        return "send inline mail successful";

    }


    @RequestMapping(value = "/sendTemplateMail", method = RequestMethod.GET)
    @ResponseBody
    public String sendTemplateMail() throws Exception {

        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
        messageHelper.setFrom("372086794@qq.com");
        messageHelper.setTo("372086794@qq.com");
        messageHelper.setSubject("主题：模板邮件");
        Context context = new Context();
        context.setVariable("message", "这是一段模板替换内容");


        String message = templateEngine.process("mailTemplate", context);
        messageHelper.setText(message, true);
        mailSender.send(mimeMessage);

        return "send template mail successful";
    }


}